﻿package mortal.game.resource
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.game.model.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;

    public class SceneConfig extends Object
    {
        private var _rankStr:String;
        private var _map:Dictionary;
        private var _mapArray:Array;
        private var _refreshMap:Array;
        private var _sceneEffect:Array;
        private var _npcMap:Dictionary;
        private static var _instance:SceneConfig;

        public function SceneConfig()
        {
            var _loc_1:int = 0;
            this._rankStr = Language.getString(11063);
            this._map = new Dictionary();
            this._npcMap = new Dictionary();
            if (_instance != null)
            {
                throw new Error(" SceneConfig 单例 ");
            }
            this._mapArray = GameDefConfig.instance.getLoadMapConfig(1);
            var _loc_2:int = 0;
            while (_loc_2 < this._mapArray.length)
            {
                
                _loc_1 = int(this._mapArray[_loc_2]);
                this.addMiniMapInfo(_loc_1);
                this._mapArray[_loc_2] = _loc_1;
                _loc_2++;
            }
            this._refreshMap = GameDefConfig.instance.getLoadMapConfig(2);
            var _loc_3:int = 0;
            while (_loc_3 < this._refreshMap.length)
            {
                
                this._refreshMap[_loc_3] = int(this._refreshMap[_loc_3]);
                _loc_3++;
            }
            this._sceneEffect = GameDefConfig.instance.getLoadMapConfig(3);
            var _loc_4:int = 0;
            while (_loc_4 < this._sceneEffect.length)
            {
                
                this._sceneEffect[_loc_4] = int(this._sceneEffect[_loc_4]);
                _loc_4++;
            }
            this.init();
            return;
        }// end function

        private function addMiniMapInfo(param1:int) : void
        {
            var _loc_2:* = param1 + "_mini.jpg";
            var _loc_3:* = new MapImgInfo();
            _loc_3.name = _loc_2;
            _loc_3.mapID = param1;
            var _loc_4:* = _loc_2;
            var _loc_5:* = GameMapConfig.instance.getMapInfo(_loc_3.mapID);
            _loc_3.loaclPath = PathConst.mapLocalPath.substr(0, (PathConst.mapLocalPath.length - 1)) + "_min/" + _loc_4;
            _loc_3.time = _loc_5.version;
            _loc_3.path = PathConst.mapPath.substr(0, (PathConst.mapPath.length - 1)) + "_min/" + _loc_4 + "?v=" + _loc_3.time;
            _loc_3.type = ".jpg";
            ResourceManager.addResource(_loc_3);
            return;
        }// end function

        public function getImageInfo(param1:int) : ImageInfo
        {
            var _loc_2:String = null;
            if (param1 > 0)
            {
                param1 = this.getShouldLoadMapId(param1);
                _loc_2 = param1 + "_mini.jpg";
                return ResourceManager.getInfoByName(_loc_2) as ImageInfo;
            }
            return null;
        }// end function

        private function getShouldLoadMapId(param1:int) : int
        {
            var _loc_2:* = GameMapConfig.instance.getMapInfo(param1);
            if (_loc_2 == null)
            {
                Alert.show("求策划配置gameMap.xml, 地图id=" + param1);
                return param1;
            }
            return _loc_2.mapscene;
        }// end function

        public function get allMap() : Dictionary
        {
            return this._map;
        }// end function

        public function init() : void
        {
            var _loc_3:int = 0;
            var _loc_1:* = this._mapArray.length;
            var _loc_2:uint = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this._mapArray[_loc_2];
                this._map[_loc_3] = this.initSceneInfo(_loc_3);
                _loc_2 = _loc_2 + 1;
            }
            return;
        }// end function

        public function getSceneInfo(param1:int) : SceneInfo
        {
            return this._map[param1] as SceneInfo;
        }// end function

        public function getMapName(param1:int) : String
        {
            var _loc_2:* = this._map[param1] as SceneInfo;
            if (_loc_2)
            {
                return _loc_2.sMapDefine.name;
            }
            return "";
        }// end function

        protected function initSceneInfo(param1:Object) : SceneInfo
        {
            var _loc_3:Object = null;
            var _loc_2:* = this._map[param1] as SceneInfo;
            if (_loc_2 == null)
            {
                _loc_3 = ConfigManager.instance.getJSONByFileName(param1 + "_scene.json");
                if (_loc_3)
                {
                    _loc_2 = new SceneInfo();
                    _loc_2.readObj(_loc_3);
                    if (this._refreshMap.indexOf(param1) != -1)
                    {
                        _loc_2.readBossRefreshPoint(ConfigManager.instance.getJSONByFileName(param1 + "_BossRefreshPoint.json"));
                    }
                    if (this._sceneEffect.indexOf(param1) != -1)
                    {
                        _loc_2.readSceneEffect(ConfigManager.instance.getJSONByFileName(param1 + "_sceneEffect.json"));
                    }
                }
                else
                {
                    throw new Error("未知的地图编码：" + param1);
                }
            }
            return _loc_2;
        }// end function

        public function getSceneInfoByNpcId(param1:int) : SceneInfo
        {
            var _loc_2:SceneInfo = null;
            var _loc_3:int = 0;
            var _loc_4:NPCInfo = null;
            for each (_loc_3 in this._mapArray)
            {
                
                _loc_2 = this.getSceneInfo(_loc_3);
                for each (_loc_4 in _loc_2.npcInfos)
                {
                    
                    if (_loc_4.tnpc != null && _loc_4.tnpc.code == param1)
                    {
                        return _loc_2;
                    }
                }
            }
            return null;
        }// end function

        public function getSceneInfoByEffectKey(param1:String) : SceneInfo
        {
            var _loc_2:SceneInfo = null;
            var _loc_3:int = 0;
            var _loc_4:SceneEffectData = null;
            for each (_loc_3 in this._mapArray)
            {
                
                _loc_2 = this.getSceneInfo(_loc_3);
                for each (_loc_4 in _loc_2.effectInfos)
                {
                    
                    if (_loc_4.key == param1)
                    {
                        return _loc_2;
                    }
                }
            }
            return null;
        }// end function

        public function setSceneEffectIsAddStageByInfo(param1:SceneEffectData, param2:Boolean) : void
        {
            return;
        }// end function

        public function getBossRefreshInfo(param1:int, param2:int, param3:Boolean = false) : BossRefreshInfo
        {
            var _loc_6:BossRefreshInfo = null;
            var _loc_8:BossRefreshInfo = null;
            var _loc_11:BossRefreshInfo = null;
            var _loc_12:Array = null;
            var _loc_13:TBoss = null;
            var _loc_4:* = this.getSceneInfo(param1);
            if (_loc_4 == null)
            {
                return null;
            }
            var _loc_5:* = _loc_4.bossRefreshs;
            if (_loc_5 == null || _loc_5.length == 0)
            {
                return null;
            }
            var _loc_7:int = 0;
            var _loc_9:* = int.MAX_VALUE;
            var _loc_10:int = 0;
            while (_loc_10 < _loc_5.length)
            {
                
                _loc_11 = _loc_5[_loc_10] as BossRefreshInfo;
                _loc_12 = _loc_11.bosses;
                if (_loc_12 == null || _loc_12.length == 0)
                {
                }
                else
                {
                    for each (_loc_13 in _loc_12)
                    {
                        
                        if (_loc_13.level > _loc_7)
                        {
                            _loc_7 = _loc_13.level;
                            _loc_6 = _loc_11;
                        }
                        if (_loc_13.level >= param2)
                        {
                            if (!param3)
                            {
                                return _loc_11;
                            }
                            if (_loc_13.level < _loc_9)
                            {
                                _loc_9 = _loc_13.level;
                                _loc_8 = _loc_11;
                            }
                        }
                    }
                }
                _loc_10++;
            }
            if (param3 && _loc_8 != null)
            {
                return _loc_8;
            }
            return _loc_6;
        }// end function

        public function getNpcInfo(param1:int, param2:int = -1) : NPCInfo
        {
            var _loc_3:SceneInfo = null;
            var _loc_4:NPCInfo = null;
            if (param2 != -1)
            {
                _loc_3 = this.getSceneInfo(param2);
                return _loc_3.getNpcInfo(param1);
            }
            for each (param2 in this._mapArray)
            {
                
                _loc_3 = this.getSceneInfo(param2);
                for each (_loc_4 in _loc_3.npcInfos)
                {
                    
                    if (_loc_4.tnpc != null && _loc_4.tnpc.code == param1)
                    {
                        return _loc_4;
                    }
                }
            }
            return null;
        }// end function

        public function getPassToInfoByTargetMapId(param1:int) : SPassTo
        {
            var _loc_2:SceneInfo = null;
            var _loc_3:Array = null;
            var _loc_4:SPassPoint = null;
            var _loc_5:SPassTo = null;
            var _loc_6:SPassTo = null;
            for each (_loc_2 in this._map)
            {
                
                _loc_3 = _loc_2.passInfos;
                if (_loc_3 == null)
                {
                    continue;
                }
                for each (_loc_4 in _loc_3)
                {
                    
                    if (_loc_4.passTo == null || _loc_4.passTo[0] == null)
                    {
                        continue;
                    }
                    _loc_5 = _loc_4.passTo[0];
                    if (_loc_5.mapId == param1)
                    {
                        _loc_6 = new SPassTo();
                        _loc_6.toPoint = new SPoint();
                        _loc_6.toPoint.x = _loc_4.point.x;
                        _loc_6.toPoint.y = _loc_4.point.y;
                        _loc_6.mapId = _loc_4.mapId;
                        return _loc_6;
                    }
                }
            }
            return null;
        }// end function

        public function getEffectInfo(param1:String, param2:int = -1) : SceneEffectData
        {
            var _loc_3:SceneInfo = null;
            var _loc_4:SceneEffectData = null;
            if (param2 != -1)
            {
                _loc_3 = this.getSceneInfo(param2);
                return _loc_3.getEffectData(param1);
            }
            for each (param2 in this._mapArray)
            {
                
                _loc_3 = this.getSceneInfo(param2);
                for each (_loc_4 in _loc_3.effectInfos)
                {
                    
                    if (_loc_4.effectName == param1)
                    {
                        return _loc_4;
                    }
                }
            }
            return null;
        }// end function

        public static function get instance() : SceneConfig
        {
            if (_instance == null)
            {
                _instance = new SceneConfig;
            }
            return _instance;
        }// end function

    }
}
